<?php
class C_user extends CI_Controller{
    public function __construct() {
        parent::__construct();
        //$this->load->helper('url');
        //$this->load->library('session');
    }
    public function login(){
        //kiem tra xem da login lam nao chua khi login da luu vao bien session roi nen kt session
        $session = $this->session->all_userdata();
        $data_user=array();
        if(isset($session['first_name'])){
            redirect('c_homepage');
        }else{
        //kiem tra dau vao post
        //print_r($_POST);
        if(isset($_POST['email'])&& isset($_POST['password'])){
            $email = $_POST['email'];
            $pass = $_POST['password'];
            $this->load->model('M_user');
            $dt= $data_user['user'] = $this->M_user->kt_user($email, $pass);
            //print_r($dt);
            if(isset($dt[0]['email'])&& $dt[0]['email']==$email){
                $newdata = array(
                   'first_name'  => $dt[0]['first_name'],
                   'user_level'     => $dt[0]['user_level']
               );

            $this->session->set_userdata($newdata);
//            $test = $this->session->all_userdata();
//            print_r($test);

                //$this->load->helper('url');
                redirect('c_homepage');
            }else{
               // $mg1 = "dfsf";
               // $this->set("mg1",$mg1);
                $data_user['mg']= "xit nhe";
            
            }
        }
        $this->load->view('v_login',$data_user);
        }
    }
    public function logout(){
        //$this->session->unset_all_userdata();
        //$test = $this->session->all_userdata();
       // print_r($test);
    $this->session->sess_destroy();
    $this->session->sess_create();
    
    redirect('c_homepage');
    }
    public function manager(){
        $this->load->view('v_manager');
    }
    
         public function register() {
        $this->load->library('form_validation');
//        $this->form_validation->set_rules('first_name', 'First name', 'required|max_length[12]');
//        $this->form_validation->set_rules('last_name', 'Last name', 'required|max_length[12]');
//        $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
//        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
//        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
//        $this->form_validation->set_message('first_name', 'dfgdfg Message');
        if ($this->form_validation->run('register') == FALSE) {
            $this->load->view('v_register');
        } else {
            //print_r($set_value);
          // echo set_value('first_name');
           // $first_name1 = $set_value['first_name'];
            $first_name = $_POST['first_name'];
            $last_name = $_POST['last_name'];
            $email = $_POST['email'];
            $pass = $_POST['password'];
            $this->load->model('M_user');
            $data['flag'] = $this->M_user->register($first_name,$last_name,$email,$pass);
            if($data['flag']==1){
                $this->load->model('M_user');
                $user = $this->M_user->select($email);
                $newdata = array(
                   'first_name'  => $first_name,
                   'user_level'     => $user[0]['user_level']
               );
               $this->session->set_userdata($newdata);
               redirect('c_homepage');
            }
        }
    }

}
?>
